package simple

import console.Console
import groovy.xml.MarkupBuilder
import simple.entity.Action
import simple.entity.Book
import simple.entity.Borrower
import simple.entity.Library

/**
 * simple libary manage progrborroweramer with c feeling, implement with list
 */
def libiray = [['test01', '安徒生'], ['test02', '格林童话']]
println libiray

def addBook(lib, book) {
    lib << book
}

def addBook(lib, ...book) {
    libiray.addAll book
}

def hasLendBook(lib, book){
    lib.contains(book)
}


def printLib(lib) {
    println lib
}

def removeBook(lib, bookName) {
    lib.removeAll { it[1] == bookName }
}

def removeBook(lib, Closure closure) {
    lib.removeAll closure
}

def countBookSize(lib) {
    println lib.size
}

def searchBook(lib, keys) {
    def result
    if (keys && keys[0] && keys[1]) {
        result = lib.findAll { it[0].contains(keys[0]) && it[1].contains(keys[1]) }
    } else if (keys && keys[1]) {
        result = lib.findAll { it[1].contains(keys[1]) }
    } else if (keys && keys[0]) {
        result = lib.findAll { it[0].contains(keys[0]) }
    }
    printLib result
}

addBook(libiray, ['test03', '百科全书'])
addBook(libiray, ['test04', '我不'])
addBook(libiray, ['test05', '媒体的本质'])
addBook(libiray, ['test06', '识人术'])
addBook(libiray, ['test07', '走近科学'])
removeBook(libiray, '安徒生')
printLib(libiray)
countBookSize libiray
searchBook libiray, ['test', '科']

// upgrade liberay implement with map
def lib2 = ['test01':['book1','book3'],'test02':['book2','book4']]

def addBook2(lib, keys){
    if (keys && keys[0] && keys[1]) {
        if (lib.containsKey(keys[0])){
             lib[keys[0]] = lib[keys[0]] << keys[1]
        }else{
             lib[keys[0]] = [keys[1]]
        }
    } else{
        throw new Exception('format error.')
    }
}

def hasLendBook2(lib, keys){
    println "${keys[0]} has borrowed ${keys[1]}?${lib[keys[0]].contains(keys[1])}"
}

def libSize(lib) {
    println "Number of borrowers in the library:${lib.size()}"
}

def libKey(lib) {
    println "Library:${lib.keySet().sort()}"
}

def countBorrowBookSize(lib, name) {
    println "Number of books borrowed by ${name}:${lib[name].size()}"
}

def printBook2(lib){
    println "Library:${lib}"
}
addBook2(lib2, ['Sally', 'Basic'])
addBook2(lib2, ['test02', '00D'])
printBook2 lib2
hasLendBook2(lib2, ['test02','00D'])
libSize lib2
libKey lib2
countBorrowBookSize lib2,'test02'

println '=== implements library using oo'

def slashy() {
    println '==================='
}


//create a library obj
def lib3 = new Library(name: 'Dunning')
   //create some book
def bk1 = new Book(catalogNumber: '111', title: 'Groovy', author: 'Ken')
def bk2 = new Book(catalogNumber: '222', title: 'OOD', author: 'Ken')
def bk3 = new Book(catalogNumber: '333', title: 'UML', author: 'John')

lib3.addBook(bk1)
lib3.addBook(bk2)
lib3.addBook(bk3)
// see stock
lib3.displayStock()

bo1 = new Borrower(membershipNumber: '1234',name: 'Jessie')
bo2 = new Borrower(membershipNumber: '5678',name: 'Sally')

lib3.registerBorrower(bo1)
lib3.registerBorrower(bo2)

// see borrowers
lib3.displayBorrowers()

lib3.displayBooksAvailableForLoan()
lib3.lendBook('111','1234')

lib3.displayBooksAvailableForLoan()
lib3.displayBooksOnLoan()
lib3.displayBorrowers()
lib3.returnBook('111')
lib3.displayBooksAvailableForLoan()
lib3.displayBooksOnLoan()
lib3.displayBorrowers()